<html>
<head><meta charset="utf-8"><title>Storing address of static in const · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Storing.20address.20of.20static.20in.20const.html">Storing address of static in const</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="236794576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Storing%20address%20of%20static%20in%20const/near/236794576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Storing.20address.20of.20static.20in.20const.html#236794576">(Apr 30 2021 at 07:06)</a>:</h4>
<p>Which nightly feature do I have to enable to allow this? The error message does not say</p>
<div class="codehilite"><pre><span></span><code>static A: i32 = 42;
const P: *const i32 = &amp;A;
</code></pre></div>



<a name="236822655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Storing%20address%20of%20static%20in%20const/near/236822655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Storing.20address.20of.20static.20in.20const.html#236822655">(Apr 30 2021 at 11:50)</a>:</h4>
<p>there's no feature for this</p>



<a name="236824838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Storing%20address%20of%20static%20in%20const/near/236824838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Storing.20address.20of.20static.20in.20const.html#236824838">(Apr 30 2021 at 12:12)</a>:</h4>
<p>I see. Thanks. Any idea why this is not allowed ? C++ supports this, e.g.,</p>
<div class="codehilite"><pre><span></span><code>extern int A;
constexpr int* p = &amp;a;
</code></pre></div>
<p>but I was wondering how sound this is. It appears to be sound in C++ at least.</p>



<a name="236824954"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Storing%20address%20of%20static%20in%20const/near/236824954" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Storing.20address.20of.20static.20in.20const.html#236824954">(Apr 30 2021 at 12:13)</a>:</h4>
<p>C++ doesn't have distinct <code>static</code> and <code>const</code> items, I think all globals are equivalent to Rust's <code>static</code> there</p>



<a name="236825077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Storing%20address%20of%20static%20in%20const/near/236825077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Storing.20address.20of.20static.20in.20const.html#236825077">(Apr 30 2021 at 12:14)</a>:</h4>
<p>But C++ constexpr can be used in constant positions like array lengths, right?</p>



<a name="236825508"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Storing%20address%20of%20static%20in%20const/near/236825508" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Storing.20address.20of.20static.20in.20const.html#236825508">(Apr 30 2021 at 12:19)</a>:</h4>
<p>Right but you can't cast a pointer to an integer at compile-time</p>



<a name="236825513"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Storing%20address%20of%20static%20in%20const/near/236825513" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Storing.20address.20of.20static.20in.20const.html#236825513">(Apr 30 2021 at 12:19)</a>:</h4>
<p>Ah, I didn't see the <code>constexpr</code> example. I'm not sure what the semantics of that is. What happens if the bits of <code>p</code> are inspected at compiletime?</p>



<a name="236825558"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Storing%20address%20of%20static%20in%20const/near/236825558" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Storing.20address.20of.20static.20in.20const.html#236825558">(Apr 30 2021 at 12:19)</a>:</h4>
<p>You already can't cast pointers to usize in const eval in rust</p>



<a name="236825571"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Storing%20address%20of%20static%20in%20const/near/236825571" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Storing.20address.20of.20static.20in.20const.html#236825571">(Apr 30 2021 at 12:19)</a>:</h4>
<p>so this seems like it might be implementable</p>



<a name="236826318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Storing%20address%20of%20static%20in%20const/near/236826318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Storing.20address.20of.20static.20in.20const.html#236826318">(Apr 30 2021 at 12:26)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![feature(linkage)]</span><span class="w"></span>

<span class="k">use</span><span class="w"> </span>::<span class="n">std</span>::<span class="n">os</span>::<span class="n">raw</span>::<span class="n">c_int</span><span class="p">;</span><span class="w"></span>

<span class="k">extern</span><span class="w"> </span><span class="s">"C"</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// static mut A: c_int;</span>
<span class="w">    </span><span class="cp">#[link_name = </span><span class="s">"A"</span><span class="cp">]</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[linkage = </span><span class="s">"external"</span><span class="cp">]</span><span class="w"></span>
<span class="w">    </span><span class="k">static</span><span class="w"> </span><span class="n">p</span>: <span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">c_int</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<ul>
<li><a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=83e21e90f3e6d455488b495cba5853a3">Playground</a></li>
</ul>



<a name="236826719"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Storing%20address%20of%20static%20in%20const/near/236826719" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Storing.20address.20of.20static.20in.20const.html#236826719">(Apr 30 2021 at 12:30)</a>:</h4>
<p>Wow I didn't know you can spawn sub-processes on the playground, good to know <span aria-label="thumbs up" class="emoji emoji-1f44d" role="img" title="thumbs up">:thumbs_up:</span> .</p>



<a name="236828077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Storing%20address%20of%20static%20in%20const/near/236828077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Storing.20address.20of.20static.20in.20const.html#236828077">(Apr 30 2021 at 12:43)</a>:</h4>
<p>Yeah, there are good odds to time out when doing so, but when it works, it is quite nice to be able to test "meta" setups <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span> </p>
<hr>
<p>By the way, on <code>--release</code> at least, using <code>&amp;A</code> for an <code>extern { static A... }</code> and using <code>p</code> where it's been assigned the address of <code>A</code> through <code>linkage = "external"</code> yields the same assembly:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">extern</span><span class="w"> </span><span class="s">"C"</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">static</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">A</span>: <span class="nc">c_int</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[link_name = </span><span class="s">"A"</span><span class="cp">]</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[linkage = </span><span class="s">"external"</span><span class="cp">]</span><span class="w"></span>
<span class="w">    </span><span class="k">static</span><span class="w"> </span><span class="n">p</span>: <span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">c_int</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="p">{</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">      </span>::<span class="n">core</span>::<span class="n">ptr</span>::<span class="n">read_volatile</span><span class="p">(</span><span class="n">p</span><span class="p">);</span><span class="w"></span>
<span class="w">      </span>::<span class="n">core</span>::<span class="n">ptr</span>::<span class="n">read_volatile</span><span class="p">(</span><span class="o">&amp;</span><span class="n">A</span><span class="p">);</span><span class="w"></span>
<span class="p">}}</span><span class="w"></span>
</code></pre></div>
<div class="codehilite" data-code-language="GAS"><pre><span></span><code><span class="nl">_ZN7example4main17hc5ea6c58e81a25c4E:</span>
    <span class="na">.cfi_startproc</span>
    <span class="nf">movq</span>    <span class="no">A@GOTPCREL</span><span class="p">(</span><span class="nv">%rip</span><span class="p">),</span> <span class="nv">%rax</span>
    <span class="nf">movl</span>    <span class="p">(</span><span class="nv">%rax</span><span class="p">),</span> <span class="nv">%ecx</span>
    <span class="nf">movl</span>    <span class="p">(</span><span class="nv">%rax</span><span class="p">),</span> <span class="nv">%eax</span>
    <span class="nf">retq</span>
</code></pre></div>
<ul>
<li><a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=17dc856c60efc0ac3517a5071e519a1a">Playground</a></li>
</ul>



<a name="236828296"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Storing%20address%20of%20static%20in%20const/near/236828296" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Storing.20address.20of.20static.20in.20const.html#236828296">(Apr 30 2021 at 12:45)</a>:</h4>
<p>This means that also simply using:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="fm">macro_rules!</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="p">{()</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">extern</span><span class="w"> </span><span class="s">"C"</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">A</span>: ::<span class="n">std</span>::<span class="n">os</span>::<span class="n">raw</span>::<span class="n">c_int</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span>::<span class="n">core</span>::<span class="n">ptr</span>::<span class="n">addr_of_mut</span><span class="o">!</span><span class="p">(</span><span class="n">A</span><span class="p">)</span><span class="w"></span>
<span class="p">})}</span><span class="w"></span>
</code></pre></div>
<p>as a way to <code>#define p (&amp;A)</code> in C parlance may be a legitimate alternative (it's one that works on stable, for instance).</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>